<?php

namespace Common\Model;

use Common\Repository\PowerRepository;

class Admin extends Base
{
    protected $table = 'admin';

    protected $guarded = [];
    protected $power = [];

    //多对多关联角色
    public function admin_roles()
    {
        return $this->belongsToMany(Roles::class, 'admin_roles', 'admin_id', 'roles_id')
            ->where('status', 0);
    }

    //判断是否有权限
    public function hasRole($alias)
    {
        $admin = admin_info();
        if (empty($this->power)) {
            $this->power = PowerRepository::getPower($admin['id']);
        }
        $allAlias = array_column($this->power, 'alias');

        if (in_array($alias, $allAlias)) {
            return true;
        }

        return false;
    }

}